﻿$(document).ready(function(){

<#list columnEntitys as columnEntity>
        <#if columnEntity.search==true>
             <#if columnEntity.uiTyp=='combogrid'>
                  //查询条件：${columnEntity.entityColumnComment}
                  $("#search_${columnEntity.entityColumnNam}").combogrid(
                  		HdUtil.combogrid.${columnEntity.selectName}()
                  );
             </#if>
             <#if columnEntity.uiTyp=='combobox'>
                    //查询条件：${columnEntity.entityColumnComment}
                   $("#search_${columnEntity.entityColumnNam}").combobox(
                      HdUtil.combobox.${columnEntity.selectName}()
                   );
             </#if>
        </#if>
         <#if columnEntity.uiTyp=='combogrid'>
                  //form表单：${columnEntity.entityColumnComment}
                  $("#${de.entityNameAlias}_${columnEntity.entityColumnNam}").combogrid(
                      HdUtil.combogrid.${columnEntity.selectName}()
                  );
             </#if>
             <#if columnEntity.uiTyp=='combobox'>
                   //form表单：${columnEntity.entityColumnComment}
                   $("#${de.entityNameAlias}_${columnEntity.entityColumnNam}").combobox(
                      HdUtil.combobox.${columnEntity.selectName}()
                   );
             </#if>
     </#list>    
    var queryParams=JSON.stringify(getQueryArr("searchForm"));
    var orderStr=JSON.stringify(getOrderObject());
    $('#${de.entityNameAlias}Table').datagrid({  
    	url: ctx+'/${de.entityName}/get${de.entityName}List',       
        fit: true,
        striped: true,
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        pageSize: 20,   
        toolbar: '#${de.entityNameAlias}ToolBar',
        fitColumns:false,
        checkOnSelect:false,
        selectOnCheck:false,
        //最好不要设定idField，否则getChecked会有问题
        //idField:'${de.keyColumn}',
        queryParams: {
            conditions: queryParams,
            orderStr:orderStr,
            //刚进入不查询，返回一个空list
            isQuery:'false'
        },
        columns:[[{
        	field:'pk',
          	checkbox:true
        },
        <#list columnEntitys as columnEntity>
        {
          field:'${columnEntity.entityColumnNam}',
          title:'${columnEntity.entityColumnComment}',
          width:80,
          sortable:true,
          align:'center',
       	  hidden:${columnEntity.hiddenL}     
        }<#if columnEntity_has_next>,</#if>
        </#list>    
        ]]
    });  
    
});

//查询
function query${de.entityName}() {
 var newqueryParams=JSON.stringify(getQueryArr("searchForm"));
 var neworderStr=JSON.stringify(getOrderObject());
    $('#${de.entityNameAlias}Table').datagrid('load', {
        conditions: newqueryParams,
         orderStr:neworderStr,
        isQuery:"true"
    });
}


function add${de.entityName}() {
	isAdd=true;
//	resetForm("${de.entityNameAlias}Form");
 	$("#${de.entityNameAlias}Form").form("clear");
  	<!--初始化一些数据   -->  
  
   	$("#${de.entityNameAlias}Dialog").dialog("open").window('center');
}

function edit${de.entityName}(rowIndex, row) {
	isAdd=false;
	//先清空
//	resetForm("${de.entityNameAlias}Form");
	if(rowIndex!=null&&row!=null){
		//如果是表格双击编辑双击编辑
		
	}else{
		var rows = $("#${de.entityNameAlias}Table").datagrid("getChecked");
	    if (rows.length==1) {
	      row=rows[0];  	
	    }else{
	    	 HdUtil.messager.error("只能修改一条数据");
	    	  return;
	    }	    
	} 
   $("#${de.entityNameAlias}Form").form("clear").form("load", row[0]);
	<!--初始化一些数据  -->
	
	$("#${de.entityNameAlias}Dialog").dialog("open").window('center');
}


function delete${de.entityName}(){
  	var rows = $("#${de.entityNameAlias}Table").datagrid("getChecked");
  	if(rows.length<=0){
	  	HdUtil.messager.error("请选择要删除的${de.tableComment}");
	  	return;
  	}
  	
  	var delstr="";
  	for(var i=0;i<rows.length;i++){
  	delstr=delstr+rows[i]['${de.keyColumn}']+",";
  	}
  	//去掉最后的逗号
  	delstr=delstr.substring(0,delstr.length-1);
  	 //var delparams=JSON.stringify(rows);
  	 $.messager.confirm(HdUtil.constant.CONFIRM, HdUtil.constant.DELETE_CONFIRM, function (r) {
  	 	if (r) {
                $.ajax({
                    method: "post",
                    url: ctx+"/${de.entityName}/delete${de.entityName}",
                    data: {
                    	//统一用delstr
                		delstr: delstr
                    },
                    success: function (data) {
                       //在Hdutil.js处理过了
                       	query${de.entityName}();
                    }
                });
            }
  	 })
	
}

function save${de.entityName}(){
  if(!$("#${de.entityNameAlias}Form").form('validate')){
		return ;
	}
	var saveEntity=JSON.stringify(getObject("${de.entityNameAlias}Form"));
	//不使用submit提交，用ajax提交，方便统一处理
	
	$.ajax({
        url: ctx+"/${entityNam}/save${entityNam}",
        type:"post",
       data:{saveEntity:saveEntity,isAdd:isAdd},
        success: function (data) {
            //在Hdutil.js处理过了
             $('#${de.entityNameAlias}Dialog').dialog("close");
              query${de.entityName}();
    }
    })

}


function colse${entityNam}Dialog(){
  $('#${de.entityNameAlias}Dialog').dialog("close");
}


